package com.sgb.kjwl.view.ui.adapter.base

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView


abstract class HDBaseAdapter<V : SimpleViewHolder> : RecyclerView.Adapter<V>(){

    /**
     * 日志
     */
    protected var TAG = String.format("TAG:%s", javaClass.simpleName)

    /**
     * 点击事件
     */
    private var pOnItemClickListener: RecyclerContract.OnItemClickListener? = null

    /**
     * 长点击事件
     */
    private var pOnItemLongClickListener: RecyclerContract.OnItemLongClickListener? = null
    private val EMPTY_LAYOUT = Int.MAX_VALUE;

    /**
     * 空布局资源 可set设置
     */
    var emptyLayout : Int = -1

    /**
     * 点击事件 执行过程
     *
     * @param view     视图
     * @param position 位置
     */
    open fun onItemClick(view: View?, position: Int) {
        if (position < 0 || position > getDataItemCount()){
            return
        }
        //兼容处理
        pOnItemClickListener?.onItemClick(view, position)
    }

    /**
     * Long Click process
     *
     * @param view     View
     * @param position 位置
     */
    open fun onItemLongClick(view: View?, position: Int) {
        if (position < 0 || position > getDataItemCount()){
            return
        }
        //兼容处理
        pOnItemLongClickListener?.onItemLongClick(view, position)
    }

    /**
     * 回收holder 时 顺吧 回收点击事件
     */
    override fun onViewRecycled(holder: V) {
        super.onViewRecycled(holder)
        holder.clear()
    }

    abstract fun onBindDataViewHolder(holder: SimpleViewHolder, position: Int)

    /**
     * 可以对空布局做一些数据绑定处理 或者监听事件 非强制重写
     */
    open fun onBindEmptyViewHolder(holder: SimpleViewHolder, position: Int){

    }

    /**
     * 获取布局类型
     */
    open fun getDataItemViewType(position: Int):Int{
        return 0
    }

    abstract fun getDataItemCount():Int

    final override fun getItemCount(): Int {
        return if (isEmpty()) 1 else getDataItemCount()
    }

    final override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): V {
        return onCreateDataViewHolder(parent, viewType)
    }
    
    open fun onCreateDataViewHolder(parent: ViewGroup, viewType: Int): V {
        val layoutResId = if (viewType == EMPTY_LAYOUT){
            getEmptyLayoutResId(viewType)
        }else {
            getLayoutResId(viewType)
        }
        val root = if (isDataBinding(viewType)){
            val dataBinding : ViewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context),
                    layoutResId, parent, false)
            dataBinding.root
        }else{
            LayoutInflater.from(parent.context).inflate(layoutResId,parent,false)
        }
        return SimpleViewHolder(root) as V
    }

    abstract fun getLayoutResId(viewType: Int):Int

    final override fun onBindViewHolder(holder: V, position: Int) {
        holder.bindingPosition = position
        val itemViewType = getItemViewType(position)
        if (itemViewType == EMPTY_LAYOUT){
            onBindEmptyViewHolder(holder, position)
        }else {
            onBindDataViewHolder(holder, position)
        }
    }

    final override fun getItemViewType(position: Int): Int {
        return if (isEmpty()){
            EMPTY_LAYOUT
        }else{
            getDataItemViewType(position)
        }
    }

    /**
     * set点击事件
     *
     * @param onItemClickListener 点击事件监听对象
     */
    open fun setOnItemClickListener(onItemClickListener: RecyclerContract.OnItemClickListener?) {
        pOnItemClickListener = onItemClickListener
    }

    /**
     * 长点击事件
     *
     * @param pOnItemLongClickListener 点击事件监听对象
     */
    open fun setOnItemLongClickListener(onItemLongClickListener: RecyclerContract.OnItemLongClickListener?) {
        this.pOnItemLongClickListener = onItemLongClickListener
    }


    /**
     * 是否为空
     */
    open fun isEmpty():Boolean{
        val b = emptyLayout != -1 && getDataItemCount() == 0
        return b
    }

    /**
     * 是否开启 DataBinding
     *  默认开启
     */
    open fun isDataBinding(viewType: Int):Boolean{
        return true
    }

    /**
     * 空布局资源
     */
    open fun getEmptyLayoutResId(viewType: Int):Int{
        return emptyLayout
    }

    /**
     * 波及范围 较广 此功能暂不开启
     */
//    override fun onAttachedToRecyclerView(@NonNull recyclerView: RecyclerView) {
//        super.onAttachedToRecyclerView(recyclerView)
//        val lLayoutManager = recyclerView.layoutManager
//        if (lLayoutManager is GridLayoutManager) {
//            val lLayoutManager1 = lLayoutManager as GridLayoutManager
//            lLayoutManager1.spanSizeLookup = BaseSpanSizeLookup<VH>(this, lLayoutManager1.spanCount)
//        }
//    }
//
//    override fun onDetachedFromRecyclerView(@NonNull recyclerView: RecyclerView) {
//        super.onDetachedFromRecyclerView(recyclerView)
//        val lLayoutManager = recyclerView.layoutManager
//        if (lLayoutManager is GridLayoutManager) {
//            val lLayoutManager1 = lLayoutManager as GridLayoutManager
//            lLayoutManager1.spanSizeLookup = null
//        }
//    }
//
//    protected open fun getSpanSize(defSize: Int, position: Int): Int {
//        return defSize
//    }
//
//    internal class BaseSpanSizeLookup<VH : SimpleViewHolder>(private val pBaseAdapter: HDBaseAdapter<VH>, var pDefSize: Int = 0) : GridLayoutManager.SpanSizeLookup() {
//        override fun getSpanSize(position: Int): Int {
//            return pBaseAdapter.getSpanSize(pDefSize, position)
//        }
//    }
}